<?php

namespace App\Http\Controllers\Index;

use App\Http\Controllers\Controller;
use App\Models\Common\CollTown;
use App\Models\Api\ShieldCompany;
use App\Models\Common\CollArea;
use App\Models\Common\CollOption;
use App\Models\Common\CollPosition;
use App\Models\Company\BuyResume;
use App\Models\Personal\BrowseResume;
use App\Models\XS\ResumeXS;
use Illuminate\Http\Request;
use App\Models\Personal\Resume;
use Illuminate\Support\Facades\Auth;
use Style, Index, Cache, DB, Site;
use Illuminate\Pagination\LengthAwarePaginator;
use Illuminate\Pagination\Paginator;

class PersonnelController extends CommonController{
    /**
     * 二维数组查询值是否存在
     * @param $value
     * @param $array
     * @param $key
     * @return bool
     */
    protected function inToArray($value, $array, $key){
        foreach($array as $k => $v){
            if($v[$key] == $value){
                return true;
            }
        }
        return false;
    }

    /**
     * 找人才
     * @param Request $request
     * @return mixed
     */
    public function index(Request $request){

        $ages = [
            //年龄数组
            ['value' => '', 'text' => '不限'],
            ['value' => 20, 'text' => '20岁以下'], ['value' => 25, 'text' => '20~25岁'],
            ['value' => 30, 'text' => '25~30岁'], ['value' => 35, 'text' => '30~35岁'],
            ['value' => 40, 'text' => '35~45岁'], ['value' => 60, 'text' => '45岁以上']
        ];
        $this->ages_value = [
            //对应真实年龄(使用方法：$ages的value 对应 $this->ages_value 的键值,age_above表示年龄下限, age_below表示年龄上限)
            20 => ['age_above' => 0, 'age_below' => 20], 25 => ['age_above' => 20, 'age_below' => 25],
            30 => ['age_above' => 25, 'age_below' => 30], 35 => ['age_above' => 30, 'age_below' => 35],
            40 => ['age_above' => 35, 'age_below' => 45], 60 => ['age_above' => 45, 'age_below' => 100]
        ];
        //接收传递值
        $input = $request->only(
            'jobarea1', 'jobarea2', 'jobarea3', 'jobarea4', 'jobarea5',//地区
            'work_year', 'age', 'sex', 'education', 'homeaddress', 'census', 'release_date',
            'jobsort1', 'jobsort2', 'jobsort3', 'jobsort4', 'jobsort5',//工作岗位
            'industry1', 'industry2', 'industry3', 'industry4', 'industry5',//行业
            'havedetails', 'chkphoto_open', 'haveworkexp', 'sortdesc',//checkbox
            'keyword', 'keyword_type', 'release_date'//搜索
        );
        //读取地区编码
        $webareacode = Site::get('webareacode');
        //=>返回地区下编码

        if(substr($webareacode, -2, 2) != '00'){
            $areas = CollTown::getTown($webareacode);//返回乡镇地区
            $areaJson = CollArea::getCity(substr($webareacode, 0, 4)."00");
        }else{
            $areaJson = CollArea::getCity($webareacode);
            $areas = $areaJson;//返回乡镇地区
        }
        //工作经验
        $work_year = CollOption::getOption('work_year');
        //年龄
        $work_years = CollOption::getOption('work_year');
        //去除数组里的空值
        $pageInput = arrayFilter($input);

        //整理地区js json
        $areaForJson = json_encode($areaJson);

        //生成地区链接数组
        $checkarray = array($request->jobarea1, $request->jobarea2, $request->jobarea3, $request->jobarea4, $request->jobarea5);
        array_unshift($areas, ['value' => '', 'text' => "不限"]);
        foreach($areas as $k => $v){
            $area[$k]['url'] = urlPagePack($pageInput, ['jobarea1' => $v['value'], 'jobarea2' => null,
                'jobarea3' => null, 'jobarea4' => null, 'jobarea5' => null]);
            $area[$k]['text'] = $v['text'];
            if(in_array($v['value'], $checkarray) and $v['value'] != ''){
                $area[$k]['check'] = 1;
            }else{
                if($v['value'] == '' and $request->jobarea1 == ''){
                    $area[$k]['check'] = 1;
                }else{
                    $area[$k]['check'] = 0;
                }
            }
        }

        foreach($checkarray as $k => $v){
            if($v != null and !$this->inToArray($v, $areas, 'value')){
                $area[] = array(
                    'url' => urlPagePack($pageInput, ['jobarea1' => $v, 'jobarea2' => null,
                        'jobarea3' => null, 'jobarea4' => null, 'jobarea5' => null]),
                    'text' => CollArea::getAreaName($v),
                    'check' => 1,
                );
            }
        }

        $optionsurl['jobarea'] = urlPagePack($pageInput, ['jobarea1' => null, 'jobarea2' => null,
            'jobarea3' => null, 'jobarea4' => null, 'jobarea5' => null]);

        //工作年限
        array_unshift($work_years, ['value' => '', 'text' => "不限"]);
        foreach($work_years as $k => $v){
            $work_year[$k]['url'] = urlPagePack($pageInput, ['work_year' => $v['value']]);
            $work_year[$k]['text'] = $v['text'];
            $work_year[$k]['value'] = $v['value'];
        }

        //年龄
        foreach($ages as $k => $v){
            $age[$k]['url'] = urlPagePack($pageInput, ['age' => $v['value']]);
            $age[$k]['text'] = $v['text'];
            $age[$k]['value'] = $v['value'];
        }

        //性别
        $optionsurl['sex'] = urlPagePack($pageInput, ['sex' => null]);
        //学历
        $optionsurl['education'] = urlPagePack($pageInput, ['education' => null]);
        //现所在地
        $optionsurl['homeaddress'] = urlPagePack($pageInput, ['homeaddress' => null]);
        //户藉
        $optionsurl['census'] = urlPagePack($pageInput, ['census' => null]);
        //显示详细信息
        $optionsurl['havedetails'] = urlPagePack($pageInput, ['havedetails' => null]);
        //有照片
        $optionsurl['chkphoto_open'] = urlPagePack($pageInput, ['chkphoto_open' => null]);
        //有工作经验
        $optionsurl['haveworkexp'] = urlPagePack($pageInput, ['haveworkexp' => null]);
        //智能排序
        $optionsurl['sortdesc'] = urlPagePack($pageInput, ['sortdesc' => null]);
        //搜索关键字
        $optionsurl['keyword'] = urlPagePack($pageInput, ['keyword' => null]);
        //分页
        $optionsurl['page'] = urlPagePack($pageInput, ['page' => null]);

        //压入中文到数组
        //地区
        $cnoption['jobarea1'] = (strlen($request->jobarea1) == 8) ? CollTown::getTownName($request->jobarea1) : CollArea::getAreaName($request->jobarea1);
        $cnoption['jobarea2'] = (strlen($request->jobarea2) == 8) ? CollTown::getTownName($request->jobarea2) : CollArea::getAreaName($request->jobarea2);
        $cnoption['jobarea3'] = (strlen($request->jobarea3) == 8) ? CollTown::getTownName($request->jobarea3) : CollArea::getAreaName($request->jobarea3);
        $cnoption['jobarea4'] = (strlen($request->jobarea4) == 8) ? CollTown::getTownName($request->jobarea4) : CollArea::getAreaName($request->jobarea4);
        $cnoption['jobarea5'] = (strlen($request->jobarea5) == 8) ? CollTown::getTownName($request->jobarea5) : CollArea::getAreaName($request->jobarea5);

        $cnoption['jobarea'] = trim($cnoption['jobarea1'] . '+' . $cnoption['jobarea2'] . '+' . $cnoption['jobarea3'] .
            '+' . $cnoption['jobarea4'] . '+' . $cnoption['jobarea5'], '+');

        //行业
        $jobsort1 = ($request->jobsort1) ? CollPosition::getPosName($request->jobsort1) : '';
        $jobsort2 = ($request->jobsort2) ? CollPosition::getPosName($request->jobsort2) : '';
        $jobsort3 = ($request->jobsort3) ? CollPosition::getPosName($request->jobsort3) : '';
        $jobsort4 = ($request->jobsort4) ? CollPosition::getPosName($request->jobsort4) : '';
        $jobsort5 = ($request->jobsort5) ? CollPosition::getPosName($request->jobsort5) : '';
        $cninput['jobsort'] = trim($jobsort1 . '+' . $jobsort2 . '+'
            . $jobsort3 . '+' . $jobsort4 . '+' . $jobsort5, '+');
        $optionsurl['jobsort'] = urlPagePack($pageInput, ['jobsort1' => null, 'jobsort2' => null,
            'jobsort3' => null, 'jobsort4' => null, 'jobsort5' => null]);

        $cnoption['jobsort1'] = $jobsort1;
        $cnoption['jobsort2'] = $jobsort2;
        $cnoption['jobsort3'] = $jobsort3;
        $cnoption['jobsort4'] = $jobsort4;
        $cnoption['jobsort5'] = $jobsort5;

        $cnoption['jobsort'] = $cninput['jobsort'];

        //工作年限
        $cnoption['work_year'] = CollOption::getOptionName($request->work_year, 'work_year');

        //年龄
        if(!empty($request->age)){
            foreach($age as $k => $v){
                if($v['value'] == $request->age){
                    $cnoption['age'] = $v['text'];
                }else{
                    $cnoption['age'] = '';
                }
            }
        }else{
            $cnoption['age'] = '1';
        }

        if(count($cnoption['age']) < 0){
            $cnoption['age'] = '';
        }

        //性别
        if($request->sex == 1){
            $sexvalue = "男";
        }elseif($request->sex == 2){
            $sexvalue = "女";
        }else{
            $sexvalue = '';
        }
        $cnoption['sex'] = $sexvalue;
        //学历
        $cnoption['education'] = CollOption::getOptionName($request->education, 'education');
        //现所在地
        $cnoption['homeaddress'] = CollArea::getAreaName($request->homeaddress);
        //户藉
        $cnoption['census'] = CollArea::getAreaName($request->census);

        //详细信息、有照片、有工作经验、分页、智能排序
        $cnoption['havedetails'] = '';
        $cnoption['chkphoto_open'] = '';
        $cnoption['haveworkexp'] = '';
        $cnoption['page'] = '';
        $cnoption['sortdesc'] = '';
        $cnoption['keyword_type'] = '';
        $cnoption['keyword'] = msubstr($input['keyword'], 0, 10);
        $cnoption['page'] = '';

        //生成去除条件链接
        $del_list = array();
        foreach($pageInput as $key => $value){
            $ikey = substr($key, 0, -1);
            if($ikey != 'jobarea' and $ikey != 'jobsort'){
                if($cnoption[$key]){
                    $del_list[] = array('text' => $cnoption[$key], 'url' => urlPagePack($pageInput, [$key => null]));
                }
            }else{
                if(substr($key, -1, 1) == 1){
                    $key1 = $ikey . '1';
                    $key2 = $ikey . '2';
                    $key3 = $ikey . '3';
                    $key4 = $ikey . '4';
                    $key5 = $ikey . '5';
                    $del_list[] = array('text' => msubstr($cnoption[$ikey], 0, 10), 'url' => urlPagePack($pageInput,
                        [$key1 => null, $key2 => null, $key3 => null, $key4 => null, $key5 => null]));
                }
            }
        }

        //处理数据查询
        if (\Site::get('xsOpen') && (!empty($request->keyword) || !empty($request->jobsort1))) {
            $resumeinfo=$this->xunSearch($request,$webareacode);
            //空数据,转sql搜索
            if($resumeinfo->total() < 1) $resumeinfo = $this->sqlSearch($request,$webareacode);
        } else {
            $resumeinfo=$this->sqlSearch($request,$webareacode);
        }
        $page = $resumeinfo->appends($pageInput)->links();//分页链接

        //基本信息查询
        if($resumeinfo){
            foreach($resumeinfo as $k => $value){
                //如果企业会员登陆，那么显示简历被查看的情况
                $user = auth()->guard('company')->user();
                if(!empty($user->uid)){
                    $buyResume=BuyResume::where('company_uid',$user->uid)->where('resume_id',$value->id)->first();
                    if(!empty($buyResume->id)){
                        $value->browse_log= 1;
                    }else{
                        $browseResume=BrowseResume::where('company_uid',$user->uid)->where('resume_id',$value->id)->first();
                        if(!empty($browseResume->id)){
                            $value->browse_log= 2;
                        }
                    }
                }

                $value->name = msubstr($value->name, 0, 8);

                //头像
                if(empty($value->avatar)){
                    $value->avatar = $value->sex==1?'/default/images/index/defualt_person_img.png':'/default/images/index/defualt_person_img2.png';
                    $value->is_avatar=0;
                }else{
                    $value->avatar = "/upload/personalavatar/" . $value->avatar;
                    $value->is_avatar=1;
                }
                $value->avatar=asset($value->avatar);
                //性别
                switch($value->sex){
                    case 0:
                        $value->sex = '';
                        break;
                    case 1:
                        $value->sex = '男';
                        break;
                    case 2:
                        $value->sex = '女';
                        break;
                }
                //年龄
                if(empty($value->birthday)){
                    $value->birthday = '';
                }else{
                    $value->birthday = birthday($value->birthday) . '岁';    //年龄
                }

                //现工作地
                if(empty($value->homeaddress)){
                    $value->homeaddress = '';
                }else{
                    $value->homeaddress = CollArea::getAreaName($value->homeaddress);
                }
                //学历
                if(empty($value->education)){
                    $value->education = '';
                }else{
                    $value->education = CollOption::getOptionName($value->education, 'education');
                }
                //婚姻
                if(empty($value->marital)){
                    $value->marital = '';
                }else{
                    $value->marital = CollOption::getOptionName($value->marital,'marital');
                }
                //工作经验
                if(empty($value->work_year)){
                    $value->work_year = '';
                }else{
                    $value->work_year = CollOption::getOptionName($value->work_year, 'work_year');
                }
                //意向岗位
                $value->intentionjobs = msubstr($value->intentionjobs, 0, 16);
                //工作地区  1
                if(empty($value->jobarea1)){
                    $value->jobarea1 = '';
                }else{
                    $value->jobarea1 = CollArea::getAreaName($value->jobarea1);
                }
                if(empty($value->jobarea2)){
                    $value->jobarea2 = '';
                }else{
                    $value->jobarea2 = CollArea::getAreaName($value->jobarea2);
                }
                if(empty($value->jobarea3)){
                    $value->jobarea3 = '';
                }else{
                    $value->jobarea3 = CollArea::getAreaName($value->jobarea3);
                }
                if(empty($value->jobarea4)){
                    $value->jobarea4 = '';
                }else{
                    $value->jobarea4 = CollArea::getAreaName($value->jobarea4);
                }
                if(empty($value->jobarea5)){
                    $value->jobarea5 = '';
                }else{
                    $value->jobarea5 = CollArea::getAreaName($value->jobarea5);
                }

                //更新时间
                if(empty($value->updatetime)){
                    $value->updatetime = '';
                }else{
                    $value->updatetime = substr($value->updatetime, 0, 10);
                }

                //详细信息-工作经验与学历
                if(!empty($value->id) and $input['havedetails'] == 9){

                    //工作经验
                    $resume_work_exp = DB::table('resume_work_exp')->select('id', 'company', 'starttime', 'endtime', 'post')
                        ->where('resume_id', '=', $value->id)->orderBy('resume_id', 'desc')->take(2)->get();

                    //追加工作经历到简历对象里
                    $value->withWorkExp = $resume_work_exp;

                    if(!empty($value->withWorkExp)){
                        foreach($value->withWorkExp as $work_k => $work_v){
                            if(!empty($work_v->company)){
                                $work_v->company =  ' | ' . $work_v->company;//公司名称
                            }else{
                                $work_v->company = '';
                            }

                            if(!empty($work_v->starttime) && !empty($work_v->endtime)){//工作时间
                                $work_v->starttime = substr($work_v->starttime, 0, 7);
                                $work_v->endtime = substr($work_v->endtime, 0, 7);
                                $work_v->expTime = $work_v->starttime . ' ~ ' . $work_v->endtime . ' ';
                            }else{
                                $work_v->expTime = '';
                            }
                        }
                    }

                    //教育经历
                    $resume_education_exp = DB::table('resume_education_exp')
                        ->select('school', 'starttime', 'endtime', 'speciality')
                        ->where('resume_id', '=', $value->id)->orderBy('resume_id', 'desc')->take(1)->get();

                    //追加教育经历到简历对象里
                    $value->withEduExp = $resume_education_exp;

                    if(!empty($value->withEduExp)){
                        foreach($value->withEduExp as $edu_k => $edu_v){
                            if(!empty($edu_v->school)){//学校
                                $edu_v->school = ' : ' . msubstr($edu_v->school, 0, 18) . ' ';
                            }else{
                                $edu_v->school= '';
                            }
                            if(!empty($edu_v->starttime) && !empty($edu_v->endtime)){//教育时间
                                $edu_v->starttime = substr(str_replace('-', '/', $edu_v->starttime), 0, 7);
                                $edu_v->endtime = substr(str_replace('-', '/', $edu_v->endtime), 0, 7);
                                $edu_v->timeExp = $edu_v->starttime . ' ~ ' . $edu_v->endtime . ' ';
                            }else{
                                $edu_v->timeExp = '';
                            }
                            if(!empty($edu_v->speciality)){//专业
                                $edu_v->speciality = ' | ' . msubstr($edu_v->speciality, 0, 13) . ' ';
                            }else{
                                $edu_v->speciality = '';
                            }
                        }
                    }

                }
            }
        }

        //设置标题
        $area_name = !empty($request->jobarea1) ? $cnoption['jobarea1'] : Site::get('area_name');
        $keyword = (strlen($request->keyword) > 0) ? $pageInput['keyword'] : '';
        $personal_search = (strlen($keyword) > 0) ? $area_name . $keyword . '招聘|' : '';

        //最后一页文字
        empty($request->page) ? $cpage = 1 : $cpage = $request->page;

        if($cpage == $resumeinfo->lastPage()){
            $lastpage = "<button class=\"nextbutton\"> 已是最后一页</button>";
        }else{
            $lastpage = "<a href='" . $resumeinfo->nextPageUrl() . "'><button class='nextbutton'>查看下一页</button></a>";
        }

        $resumeinfo->appends(compact('havedetails'))->links();//搜索信息添加到分页链接

        return Style::view(
            'index.personnel', compact('pageInput', 'resumeinfo', 'input', 'workArea','area', 'work_year',
                'age', 'optionsurl', 'del_list', 'cninput', 'page', 'lastpage', 'areaForJson',
                'personal_search')
        );
    }

    /** 迅搜搜索
     * @param $request
     * @param $webareacode
     * @return \Illuminate\Pagination\LengthAwarePaginator
     */
    public function xunSearch($request,$webareacode)
    {
        //查询条件
        $q = $request->all();

        //黑名单id
        $user = Auth::guard('company');
        if($user->check()) {
            $shield = ShieldCompany::where('company_uid', $user->user()->uid)->pluck('resume_id');
            if (count($shield) > 0) {
                $q['shield'] = $shield->toArray();
            }
        }

        $jobarea=[];//工作地点
        $jobsort=[];//所属岗位
        for ($i=1;$i<=5;$i++) {
            $area=$request->input('jobarea' . $i);
            $sort=$request->input('jobsort' . $i);
            if (!empty($area)) {
                $jobarea[]=$area;
            }
            if (!empty($sort)) {
                $jobsort[]=$sort;
            }
        }
        $q['jobarea']=$jobarea;
        $q['jobsort']=$jobsort;

        //更新日期
        $q['updatetime']=$request->release_date;

        //户藉
        if (!empty($request->census) && $request->census != 'other') {
            $q['census'] = $request->census;
        } elseif ($request->census == 'other') {
            $q['census'] = '';
            $q['census_other'] = $webareacode;
        }

        //年龄
        if(!empty($request->age)){
            if (array_key_exists($request->age,$this->ages_value)){
                $age = [];
                $age['age_above'] = !empty($this->ages_value[$request->age]['age_above']) ? $this->ages_value[$request->age]['age_above'] : 0;
                $age['age_below'] = !empty($this->ages_value[$request->age]['age_below']) ? $this->ages_value[$request->age]['age_below'] : 0;
                $q['age'] = $age;
            }
        }

        //关键字
        if(!empty($request->keyword)){
            if($request->keyword_type == 2){
                $q['name'] = $request->keyword;
                unset($q['keyword']);
            }elseif($request->keyword_type == 3){
                $q['intentionjobs'] = $request->keyword;
                unset($q['keyword']);
            }else{
                $q['keyword'] = $request->keyword;
            }
        }

        //创建迅搜对象
        $xs = new ResumeXS();
        //查询页码
        $page = $request->input('page', 1);
        if($page>200) $page=200;
        //搜索(参数:搜索条件,第几页,一页个数,获取总数,是否按照时间排序)
        $arr = $xs->searchResume($q, $page, 20, true);
        //分页并返回
        return  $this->pageInfo($arr['data'],$arr['total'],$request);
    }

    /**
     * 分页信息
     */
    protected function pageInfo($data,$total,$request){
        $perPage = 20;//每页显示20条
        $current_page = $request->input('page',1);
        $total=$total>4000?4000:$total;
        $company_job = new LengthAwarePaginator($data, $total, $perPage, $current_page,[
            'path' => Paginator::resolveCurrentPath(), //注释2
            'pageName' => 'page',
        ]);
        return $company_job;
    }

    /**
     * 取得分页启始数
     */
    protected function getSkip($page){
        if(empty($page)) $page=1;
        if($page>200) $page=200;
        return ($page-1)*20;
    }

    

    /** 数据库搜索
     * @param $request
     * @param $webareacode
     * @return mixed
     */
    public function sqlSearch($request,$webareacode)
    {
        $resumeinfo = Resume::select(
            'id', 'uid', 'name', 'sex', 'birthday', 'education', 'homeaddress',
            'work_year', 'intentionjobs', 'jobarea1', 'jobarea2', 'jobarea3',
            'jobarea4', 'jobarea5', 'updatetime', 'avatar', 'resume_status','marital')
            //假如企业用户登录-不显示屏蔽该企业的简历
            ->where(function($query) {
                $user = Auth::guard('company');
                if($user->check()){
                    $prefix = env("DB_PREFIX");
                    $query->whereRaw(("id NOT IN (SELECT distinct resume_id FROM ".$prefix."shieldcompanies WHERE company_uid=".$user->user()->uid.")"));
                };
            })
            //工作地点
            ->where(function($query) use ($request){
                $city = [];
                for($i = 1; $i <= 5; $i++){
                    if(!empty($request->input('jobarea' . $i))){
                        $query->orWhere('jobarea1', '=', $request->input('jobarea' . $i))
                            ->orWhere('jobarea2', '=', $request->input('jobarea' . $i))
                            ->orWhere('jobarea3', '=', $request->input('jobarea' . $i))
                            ->orWhere('jobarea4', '=', $request->input('jobarea' . $i))
                            ->orWhere('jobarea5', '=', $request->input('jobarea' . $i));
                        $city[] = substr($request->input('jobarea' . $i), 0, 4) . '00';
                    }else{
                        break;
                    }
                }
                if(count($city) > 0){
                    $city = array_unique($city);
                    foreach($city as $v){
                        $query->orWhere('jobarea1', '=', $v)
                            ->orWhere('jobarea2', '=', $v)
                            ->orWhere('jobarea3', '=', $v)
                            ->orWhere('jobarea4', '=', $v)
                            ->orWhere('jobarea5', '=', $v);
                    }
                }
            })
            //工作经验 当前所选工作经验以上
            ->where(function($query) use ($request){
                if(!empty($request->work_year)){
                    $query->where('work_year', '=', $request->work_year);
                    $query->orWhere('work_year', '=', $request->work_year+1);
                }
            })
            //年龄要求 当前所选大于或者等于年龄以上的
            ->where(function($query) use ($request){

                if(!empty($request->age)){
                    $age_above = !empty($this->ages_value[$request->age]['age_above']) ? $this->ages_value[$request->age]['age_above'] : 0;
                    $age_below = !empty($this->ages_value[$request->age]['age_below']) ? $this->ages_value[$request->age]['age_below'] : 0;

                    if($age_above == 0){
                        $greater = date('Y-m-d');
                    }else{
                        //当前用工最低年龄
                        $date = date('Y-m-d');
                        $date = date_create($date);
                        $days = $age_above * 365 + intval($age_above / 4);
                        date_sub($date, date_interval_create_from_date_string("$days days"));
                        $greater = date_format($date, "Y-m-d");
                    }
                    //当前用工最高年龄
                    if($age_below >= 0){
                        $date = date('Y-m-d');
                        $date = date_create($date);
                        $days = $age_below * 365 + intval($age_below / 4);
                        date_sub($date, date_interval_create_from_date_string("$days days"));
                        $lesstime = date_format($date, "Y-m-d");
                    }
                    $query->whereBetween('birthday', [$lesstime, $greater]);
                }

            })
            //所属岗位
            ->where(function($query) use ($request){
                for($i = 1; $i <= 5; $i++){
                    if(!empty($request->input('jobsort' . $i)))
                        $query->orWhere('jobsort1', '=', $request->input('jobsort' . $i))
                            ->orWhere('jobsort2', '=', $request->input('jobsort' . $i))
                            ->orWhere('jobsort3', '=', $request->input('jobsort' . $i))
                            ->orWhere('jobsort4', '=', $request->input('jobsort' . $i))
                            ->orWhere('jobsort5', '=', $request->input('jobsort' . $i));
                    else break;
                }
            })
            //性别要求
            ->where(function($query) use ($request){
                if(!empty($request->sex)) $query->where('sex', '=', $request->sex);
            })
            //学历要求
            ->where(function($query) use ($request){
                if(!empty($request->education)) $query->where('education', '=', $request->education);
            })
            //现居住地
            ->where(function($query) use ($request){
                if(!empty($request->homeaddress)){
                    $query->where('homeaddress', '=', $request->homeaddress);
                }
            })
            //搜索
            ->where(function($query) use ($request){
                if(!empty($request->keyword)){
                    if($request->keyword_type == 2){
                        $query->where('name', 'like', '%' . $request->keyword . '%');
                    }elseif($request->keyword_type == 3){
                        $query->where('intentionjobs', 'like', '%' . $request->keyword . '%');
                    }else{
                        //全文搜索时？？？？
                        $query->where('name', 'like', '%' . $request->keyword . '%')
                            ->orWhere('intentionjobs', 'like', '%' . $request->keyword . '%');
                    }
                }
            })
            //发布日期
            ->where(function($query) use ($request){
                if(!empty($request->release_date)){
                    //获取选中的多少时间内
                    $ctime = date("Y-m-d H:i:s", strtotime("-" . $request->release_date . "day"));
                    $query->where('updatetime', '>=', $ctime);
                }
            })
            //照片情况
            ->where(function($query) use ($request){
                if($request->chkphoto_open == 1){//0代表显示
                    $query->where('chkphoto_open', 0)
                        ->where('avatar', '<>', '');
                }
            })
            //简历状态是1--公开
            ->where('resume_status', '=', '1')
            //户藉
            ->where(function($query) use ($request, $webareacode){
                if(!empty($request->census) && $request->census != 'other'){
                    $query->where('census', '=', $request->census);
                }elseif($request->census == 'other'){
                    $homeAddressCode = substr($webareacode, 0, 4);
                    $query->where('census', 'not like', $homeAddressCode . '%');
                }
            });

        $count=$resumeinfo->count();

        //数据排序方式
        switch($request->sortdesc){
            case 1:
                $resumeinfo = $resumeinfo->orderBy('updatetime','desc')->orderBy('id','desc');
                break;
            default:
                $resumeinfo = $resumeinfo->orderBy('updatetime','desc')->orderBy('id','desc');
        }


        $resumeinfo = $resumeinfo->skip($this->getSkip($request->page))->take(20)->get();
        //分页并返回
        return  $this->pageInfo($resumeinfo,$count,$request);

    }
    
}